package start;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.*;
import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.jupiter.params.shadow.com.univocity.parsers.conversions.BigDecimalConversion;
import org.springframework.util.StringUtils;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import start.jaxb.*;
import start.util.JaxbUtil;
import start.util.XmlUtil;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.xpath.XPathConstants;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.text.ParseException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.zip.CRC32;

/**
 * 1.8时间新用法
 */
public class Java8Study02 {



    public static void main(String[] args) throws ParseException {

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            if(next == 3) {
                return;
            }
            System.out.println(next);
        }


        //String date = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        //System.out.println(date);
        //System.out.println("--------------------------------");


        /*String a = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
                "<CBSERPPGK>\n" +
                "    <INFO>\n" +
                "        <FUNNAM>ERPAYSTA</FUNNAM>\n" +
                "    </INFO>\n" +
                "    <ERPAYSTAX>\n" +
                "        <REFNBR>TEST202302130003</REFNBR>\n" +
                "        <BUSNBR>AP2000002177</BUSNBR>\n" +
                "    </ERPAYSTAX>\n" +
                "    <ERPAYSTAX>\n" +
                "        <REFNBR>TEST202302130002</REFNBR>\n" +
                "        <BUSNBR>AP2000002176</BUSNBR>\n" +
                "    </ERPAYSTAX>\n" +
                "\t<ERPAYSTAX>\n" +
                "        <REFNBR>TEST202302130006</REFNBR>\n" +
                "        <BUSNBR>AP2000002180</BUSNBR>\n" +
                "    </ERPAYSTAX>\n" +
                "</CBSERPPGK>";

        String b = CRC32Util.getCheckSumWithCRC32("123456", a);
        System.out.println(b);

        String body = "<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"yes\"?>\n" +
                "\t\t<CBSERPPGK>\n" +
                "\t\t\t<INFO>\n" +
                "\t\t\t\t<FUNNAM>ERPAYSAV</FUNNAM>\n" +
                "\t\t\t</INFO>\n" +
                "\t\t\t<APPAYSAVX>\n" +
                "\t\t\t\t<OPRTYP>202</OPRTYP>\n" +
                "\t\t\t\t<PAYSON>N</PAYSON>\n" +
                "\t\t\t\t<PAYTYP>2</PAYTYP>\n" +
                "\t\t\t\t<INNACC></INNACC>\n" +
                "\t\t\t\t<BNKTYP>CMB</BNKTYP>\n" +
                "\t\t\t\t<BUSTYP>0</BUSTYP>\n" +
                "\t\t\t\t<CCYNBR>10</CCYNBR>\n" +
                "\t\t\t\t<CLTACC>755915711010404</CLTACC>\n" +
                "\t\t\t\t<CLTNBR>0003</CLTNBR>\n" +
                "\t\t\t\t<EPTDAT></EPTDAT>\n" +
                "\t\t\t\t<EPTTIM></EPTTIM>\n" +
                "\t\t\t\t<EXTTX1>江西财礼</EXTTX1>\n" +
                "\t\t\t\t<OPRMOD>3</OPRMOD>\n" +
                "\t\t\t\t<INTDAT></INTDAT>\n" +
                "\t\t\t\t<ITMNBR></ITMNBR>\n" +
                "\t\t\t\t<PLNNBR></PLNNBR>\n" +
                "\t\t\t\t<RECNUM>1</RECNUM>\n" +
                "\t\t\t\t<REFNBR>TEST202302130006</REFNBR>\n" +
                "\t\t\t\t<REVACC>755915711010603</REVACC>\n" +
                "\t\t\t\t<REVBNK>招商银行深圳分行营业部</REVBNK>\n" +
                "\t\t\t\t<REVCIT>深圳市</REVCIT>\n" +
                "\t\t\t\t<REVEML></REVEML>\n" +
                "\t\t\t\t<REVMOB></REVMOB>\n" +
                "\t\t\t\t<REVNAM>招商银行</REVNAM>\n" +
                "\t\t\t\t<REVPRV>广东省</REVPRV>\n" +
                "\t\t\t\t<TRSAMT>688888</TRSAMT>\n" +
                "\t\t\t\t<TRSUSE>转款</TRSUSE>\n" +
                "\t\t\t\t<REVCAT></REVCAT>\n" +
                "\t\t\t\t<PAYCAT></PAYCAT>\n" +
                "\t\t\t</APPAYSAVX>\n" +
                "\t\t</CBSERPPGK>";

       *//* CBSERPPGK cbserppgk = new CBSERPPGK();
        INFO info = new INFO();
        info.setERPTYP("001");
        info.setERRMSG("广东省");
        info.setFUNNAM("摘要信息01");
        info.setRETCOD("002");
        APPAYSAVZ appaysavz = new APPAYSAVZ();
        appaysavz.setBUSNBR("001");
        appaysavz.setRECNUM("001");
        appaysavz.setREFNBR("001");
        appaysavz.setERRCODl("001");
        SYCOMRETZ sycomretz = new SYCOMRETZ();
        sycomretz.setERRMSG("001");
        sycomretz.setERRCOD("001");
        sycomretz.setERRMSG("001");

        cbserppgk.setINFO(info);
        cbserppgk.setAPPAYSAVZ(appaysavz);
        cbserppgk.setSYCOMRETZ(sycomretz);

        JaxbUtil jaxbUtil = new JaxbUtil(CBSERPPGK.class);
        String xml = jaxbUtil.toXmlNoStandalone(cbserppgk, "GBK");

        String checkCode = CRC32Util.getCheckSumWithCRC32("123456", xml);

        PGK pgk = new PGK();
        pgk.setCHECKCODE(checkCode);
        pgk.setDATA(xml);

        jaxbUtil = new JaxbUtil(PGK.class);
        String rootXml = jaxbUtil.toXmlEscapeStandalone(pgk, "GBK");

        System.out.println(rootXml);*//*

        String resultStr = null;
        try {
            PGK pgk = new PGK();
            String checkCode = CRC32Util.getCheckSumWithCRC32("123456", body);
            pgk.setDATA(body);
            pgk.setCHECKCODE(checkCode + "1");
            JaxbUtil jaxbUtil = new JaxbUtil(PGK.class);
            String queryParam = jaxbUtil.toXmlEscape(pgk, "GBK");
            resultStr = HttpRequest.post("http://192.168.110.55:6789")
                    .header(Header.CONTENT_TYPE, ContentType.XML.toString())
                    .charset("GBK")
                    .body(queryParam)
                    .timeout(100*1000)//超时，毫秒
                    .execute().body();

            //resultStr = resultStr.replaceAll("\\r", "").replaceAll("\\n","");
            //PGK pgk = convertXMLToObject(resultStr, PGK.class);
            //CBSERPPGK cbserppgk = convertXMLToObject(pgk.getDATA().trim(), CBSERPPGK.class);

            jaxbUtil = new JaxbUtil(PGK.class);
            pgk = jaxbUtil.fromXml(resultStr);
            jaxbUtil = new JaxbUtil(CBSERPPGK.class);
            CBSERPPGK cbserppgk = jaxbUtil.fromXml(pgk.getDATA().trim());

            System.out.println(cbserppgk);

        } catch (HttpException e) {
            resultStr = e.getMessage();
            e.printStackTrace();
        }*/
        //String value = XmlUtil.xmlToBean(XmlUtil.get , );
        //System.out.println(value);
        //logger.info("---响应结果：" +resultStr);

    }


    /*private static <T> T convertXMLToObject(String cdata, Class<T> clazz) {
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller un = context.createUnmarshaller();
            T pgk = (T) un.unmarshal(new InputSource(new StringReader(cdata)));
            return pgk;
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }*/



    public static String getXml(Object object) {
        JaxbUtil jaxbUtil = new JaxbUtil(object.getClass());
        return  jaxbUtil.toXml(object, "GBK");
    }




    public static List<BigDecimal> split(List<BigDecimal> list, BigDecimal limit) {
        List<BigDecimal> collect = list.stream().map(amount -> {
            if (amount.compareTo(limit) > 0) {
                List<BigDecimal> listT = Lists.newArrayList(limit);
                listT.addAll(split(Lists.newArrayList(amount.subtract(limit)), limit));
                return listT;
            } else {
                return Lists.newArrayList(amount);
            }
        }).flatMap(Collection::stream).collect(Collectors.toList());
        return collect;
    }


    public static byte[] generateAesKey(int keysize, String keyStr) {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            if (StringUtils.isEmpty(keyStr)) {
                keyGenerator.init(keysize);
            } else {
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
                secureRandom.setSeed(keyStr.getBytes());
                keyGenerator.init(keysize, secureRandom);
            }

            SecretKey secretKey = keyGenerator.generateKey();
            return secretKey.getEncoded();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void t01(){

        System.out.println("20221201".substring("20221201".length()-2,"20221201".length()));

        LocalDateTime dt = LocalDateTime.now();
        //获取当前月第一天
        int limitday = 27 ;
        int nextDayOfMonth = 1 - 1 ;
        int dayOfMonth = dt.getDayOfMonth();
        if(limitday >= dayOfMonth) {
            System.out.println("账期轮转到下个月第一天");
            System.out.println("下个月一天" + dt.plusMonths(1L).with(TemporalAdjusters.firstDayOfMonth()).plusDays(nextDayOfMonth).format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        }
        System.out.println("当前月记号" + dt.getDayOfMonth());
        System.out.println("前月第一天" + dt.with(TemporalAdjusters.firstDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy‐MM‐dd")));
        //获取当前月最后一天
        System.out.println(dt.with(TemporalAdjusters.lastDayOfMonth()));




        //获取当前时间
        LocalDate localDate = LocalDate.now();
        //获取当前月的第一天
        LocalDate firstday = localDate.with(TemporalAdjusters.firstDayOfMonth());
        //获取当前周第一天
        LocalDate firstEeekday = localDate.with(DayOfWeek.MONDAY);

        LocalDate firstDayMonth = localDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDayMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("当前日期：" + localDate);
        System.out.println("当前周第一天：" + firstEeekday);
        System.out.println("获取当前月的第一天：" + firstDayMonth);
        System.out.println("获取当前月的最后一天：" + lastDayMonth);

        //获取两时间时间差
        System.out.println("两时间之差：" + Period.between(firstDayMonth, lastDayMonth).getDays());

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy‐MM‐dd hh:mm:ss");
        System.out.println(dtf.format(dt));

    }

}
